import os 
import cv2
path=r'E:\zhujinchao\uterus\cellseg'
for i in os.listdir(path):
    ipath=os.path.join(path,i)
    # print(ipath)
    tpath=ipath+'\\'+'train'
    mpath=ipath+'\\'+'trainmask'
    for img in os.listdir(mpath):
        impath=os.path.join(mpath,img)
        # print(impath)
        im=cv2.imread(impath,0)
        ret,thresh=cv2.threshold(im,127,255,cv2.THRESH_BINARY)
        contour,heirarch=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
        if len(contour)==0:
            # print(im,len(contour))
            dimg=os.path.join(tpath,img)
            # print(dimg)
            # os.remove(dimg)
            os.remove(impath)